package cn.gjculture.shopping.api.service.impl;

import cn.gjculture.shopping.api.dao.CommonDeleteMapper;
import cn.gjculture.shopping.api.dao.CommonUpdateMapper;
import cn.gjculture.shopping.api.dao.RoutingRuleDao;
import cn.gjculture.shopping.api.dao.RoutingRuleMapper;
import cn.gjculture.shopping.api.entity.RoutingRule;
import cn.gjculture.shopping.api.service.MerchantService;
import cn.gjculture.shopping.api.service.RoutingRuleService;
import cn.gjculture.shopping.api.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class RoutingRuleServiceImpl implements RoutingRuleService {

    @Autowired
    RoutingRuleDao routingRuleDao;

    @Autowired
    RoutingRuleMapper routingRuleMapper;

    @Autowired
    CommonDeleteMapper commonDeleteMapper;
    @Autowired
    CommonUpdateMapper commonUpdateMapper;

    @Autowired
    MerchantService merchantService;


    @Override
    public int insert(List<RoutingRule> routingRules) {
       if(!CollectionUtils.isEmpty(routingRules)){
           for(RoutingRule rule:routingRules){
               rule.setRoutingRuleId(UUIDUtil.getUUID());
           }
       }
        return routingRuleMapper.insert(routingRules);
    }

    @Override
    public int update(RoutingRule routingRule) {
        return commonUpdateMapper.updateRoutingRule(routingRule);
    }

    @Override
    public List<RoutingRule> queryByGoodsId(String goodsId) {
        List<RoutingRule> routingRules=routingRuleDao.queryByGoodsId(goodsId);
        if(!CollectionUtils.isEmpty(routingRules)){
            for(RoutingRule r:routingRules){
                r.setMerchant(merchantService.queryByBelongId(r.getSupplierId()));
            }
        }
        return routingRules;
    }

    @Override
    public int deleteMore(List<String> routingRuleIds) {
        return commonDeleteMapper.deleteRoutingRule(routingRuleIds);
    }
}
